<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>发送消息</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
        content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="../css/font.css">
    <link rel="stylesheet" href="../css/iconfont.css">
    <link rel="stylesheet" href="../lib/layui/css/layui.css" media="all">

    <link rel="stylesheet" href="../css/xadmin.css">
    <script src="../lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../js/xadmin.js"></script>
    <script src="../js/jquery.min.js"></script>
    <script src="../js/base.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
          <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
          <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
    <style>
        tr td,
        tr th {
            text-align: center !important;
            border: none !important;
        }

        tr td {
            border-bottom: 1px solid #eee !important;
            border-top: 1px solid #eee !important;
        }
    </style>
</head>

<body>
    <div class="layui-fluid">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">
                    <!-- <div class="layui-card-header">数据统计</div> -->
                    <div class="layui-fluid">
                        <div class="layui-row layui-col-space15">
                            <form class="layui-form" onkeydown="if(event.keyCode==13){return false;}">
                                <div class="layui-col-md12">
                                    <div class="layui-card">
                                        <!-- 表格部分开始 -->
                                        <table class="layui-table">
                                            <colgroup>
                                                <col width="150">
                                                <col width="200">
                                                <col>
                                            </colgroup>
                                            <thead>
                                                <tr>
                                                    <th>标题</th>
                                                    <th>内容</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <tr>
                                                    <td class="biaoti"></td>
                                                    <td class="neirong"></td>
                                                </tr>
                                            </tbody>
                                        </table>
                                        <!-- 表格结束 -->
                                    </div>
                                    <div class="layui-input-block">
                                        <!-- 消息类型： -->
                                        <input type="radio" name="messageType" value="1" title="网站消息" lay-filter="layui_msgType" checked>
                                        <input type="radio" name="messageType" value="2" title="oa消息" lay-filter="layui_msgType">
                                    </div>
                                    <div class="layui-form-item" style="text-align: center">
                                        <button type="submit" lay-filter="sendAll" lay-submit="" class="layui-btn sendAll">全部发送</button>
                                        <button type="submit" lay-filter="add1" lay-submit="" class="layui-btn send1">发送</button>
                                        <button type="button" class="layui-btn layui-btn-primary" onclick="xadmin.close()">取消</button>
                                    </div>
                                </div>
                                <div class="layui-col-md12">
                                        <div class="layui-card">
                                            <!-- <div class="layui-card-header">数据统计</div> -->
                                            <div class="layui-fluid">
                                                <div class="layui-row layui-col-space15">
                                                    <div class="layui-col-md12">
                                                        <div class="layui-card-body ">
                                                            <form class="layui-form" action="" >
                                                                <div class="layui-inline layui-show-xs-block">
                                                                    <input class="layui-input" autocomplete="off" placeholder="姓名" name="name" id="name">
                                                                </div>
                                                                <div class="layui-inline layui-show-xs-block">
                                                                    <input class="layui-input" autocomplete="off" placeholder="电话" name="phone" id="phone">
                                                                </div>
                                                                <div class="layui-inline layui-show-xs-block">
                                                                    <button class="layui-btn search" data-type="reload" lay-filter="search">搜索</button>
                                                                </div>
                                                            </form>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    <div class="layui-card">
                                        <!-- 表格部分开始 -->
                                        <table id="sendMember_table" lay-data="{id: 'idTest'}" lay-filter="test"></table>
                                        <!-- 表格结束 -->
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>
    </div>
</body>

</html>

<script>
    layui.use(['table','form'], function () {
        var table = layui.table,
            form = layui.form;

        var userList = ""
        table.render({
            elem: '#sendMember_table', 
            height: 312,
            method:'post',
            // dataType: "json",
            url: getUserIdHaveSessionId, //数据接口
            page: true, //开启分页 
            cols: [[ //表头
                { type: 'checkbox', fixed: 'left' }, 
                { field: 'name', title: '姓名', width: 160 }, 
                { field: 'tel', title: '电话', width: 160 }, 
            ]],
            id:'idTest' ,
            done:function(res){
                userList = res.data
                console.log(res,"输出用户列表")
            }
        });

        //头部输入框筛选功能
        var $ = layui.$, active = {

            reload: function () {

                var name = $('#name');
                var phone = $("#phone")
                // 执行重载（重新加载数据表格）
                table.reload('idTest', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    },
                    where: {
                        "name": name.val(),
                        "tel": phone.val()
                    }
                }, 'data');
            }
        };


        //给查询添加点击事件
        $('.search').on('click', function () {
            var type = $(this).data('type');
            // console.log(active[type])
            active[type] ? active[type].call(this) : '';
            //一定要阻止表单的默认行为（刷新）
            return false
        });
        var messageType = '';
        form.on('radio(layui_msgType)', function(data){
            // console.log(data.elem); //得到radio原始DOM对象
            // console.log(data.value); //被点击的radio的value值
            messageType = data.value
        }); 
        
        $(".sendAll").on("click", function () {
            // console.log(111)
            var userArr = []
            for(var i = 0; i < userList.length; i++){
                userArr.push(userList[i].id)
            }

            layer.confirm('确认全部发送吗?', { icon: 3, title: '提示' }, function (index) {
                //do something
                $.ajax({
                    type: "get",
                    url: sendMsg,
                    data: {
                        id: $(".send1").attr("index"),
                        userId: JSON.stringify(userArr),
                        messageType:messageType?messageType:1,//消息类型：1网站消息，2oa消息
                    },
                    success: function (res) {

                        if (res.msg == 0) {
                            // console.log(res)
                            layer.alert("发送成功", { icon: 6 }, function () {
                                // var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                // parent.layer.close(index); //再执行关闭
                                //关闭当前frame
                                xadmin.close();

                                // 可以对父窗口进行刷新 
                                xadmin.father_reload();
                            })
                        } else {
                            console.log(res.msg)
                        }
                    },
                    error: function (error) {
                        console.log(error)
                    }
                })
                layer.close(index);
            });
            return false
        })
        form.on('submit(add1)', function (data,index) {
            // console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
            // console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            var checkStatus = table.checkStatus('idTest');
            console.log(checkStatus.data, "监听的表格选中行")
            var userId = []
            for(var i = 0; i < checkStatus.data.length; i ++){
                userId.push(checkStatus.data[i].id)
            }
            // console.log(userId,"选中的用户集体")
            $.ajax({
                type:"get",
                url: sendMsg,
                data:{
                    id:$(".send1").attr("index"),
                    userId:JSON.stringify(userId),
                    messageType:messageType?messageType:1,//消息类型：1网站消息，2oa消息
                },
                success:function(res){
                    
                    if(res.msg == 0){
                        // console.log(res)
                        layer.alert("发送成功",{icon:6},function(){
                            // var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            // parent.layer.close(index); //再执行关闭
                             //关闭当前frame
                            xadmin.close();

                            // 可以对父窗口进行刷新 
                            xadmin.father_reload();
                        })
                    } else {
                        console.log(res.msg)
                    }
                },
                error:function(error){
                    console.log(error)
                }
            })
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
    });
       
</script>